data "google_billing_account" "account" {
  billing_account = "<%= ctx[:test_env_vars]['billing_acct'] -%>"
}

data "google_project" "project" {
}

resource "google_billing_budget" "<%= ctx[:primary_resource_id] %>" {
  billing_account = data.google_billing_account.account.id
  display_name    = "<%= ctx[:vars]['budget_name'] %>"

  budget_filter {
    projects = ["projects/${data.google_project.project.number}"]
  }

  amount {
    specified_amount {
      currency_code = "USD"
      units         = "100000"
    }
  }

  threshold_rules {
    threshold_percent = 1.0
  }
  threshold_rules {
    threshold_percent = 1.0
    spend_basis       = "FORECASTED_SPEND"
  }
  
  all_updates_rule {
    monitoring_notification_channels = [
      google_monitoring_notification_channel.notification_channel.id,
    ]
    disable_default_iam_recipients = true
  }
}

resource "google_monitoring_notification_channel" "notification_channel" {
  display_name = "<%= ctx[:vars]['channel_name'] %>"
  type         = "email"
  
  labels = {
    email_address = "address@example.com"
  }
}